package logic

import (
	"MyBlogv2/Servers/Article/article"
	"MyBlogv2/Servers/Article/internal/svc"
	"MyBlogv2/blog-common/models"
	"context"
	"github.com/jinzhu/copier"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetArticleEchoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetArticleEchoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetArticleEchoLogic {
	return &GetArticleEchoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetArticleEchoLogic) GetArticleEcho(in *Article.ArticleRequest) (*Article.GetArticleEchoResponse, error) {
	id := in.Id
	var articleDto models.ArticleDto
	models.Db.Model(new(models.ArticleDto)).Where("id = ?", id).Find(&articleDto)
	var tagId []int
	models.Db.Model(new(models.ArticleTag)).Distinct("tag_id").Select("tag_id").Where("article_id = ?", id).Find(&tagId)
	articleDto.TagId = tagId
	var articleDtoResponse Article.ArticleDto
	copier.Copy(&articleDtoResponse, &articleDto)
	return &Article.GetArticleEchoResponse{
		Code: 200,
		Data: &articleDtoResponse,
	}, nil
}
